package sf;
public class CycleQueen<T> {
	private int font=0;
	private int rear=0;
	private int MaxSize;
	private T[] datas;
	public CycleQueen(int MaxSize){
		this.MaxSize=MaxSize+1;//MaxSize为数组长度,该循环队列实现方式需要浪费一个空间
		datas =  (T[]) new Object[MaxSize+1];
	}
	public boolean isEmpty() {
		return font==rear;
	}
	public int Size() {
		return (rear-font+MaxSize)%MaxSize;
	}
	public void push(T data) {
		if (font==((rear+1)%MaxSize))
			throw new RuntimeException("full");
		rear = (rear+1)%MaxSize;
		datas[rear] = data;
	}
	public T poll() {
		if (isEmpty())
			throw new RuntimeException("empty");
		font = (font+1)%MaxSize;
		T data = datas[font];
		datas[font] = null;
		return data;
	}
	public void show() {
		for(T data:datas) {
			System.out.print(data+"\t");			
		}
		System.out.print("size:"+datas.length+" maxSize:"+MaxSize+" eme:"+Size());
	}
	public static void main(String[] args) {
		int size = 10;
		CycleQueen<Integer> queen = new CycleQueen<>(size);
		System.out.println("\n================================================================================================================================================================");
		queen.show();
		System.out.println();
		for(int i=0; i<size;i++) {
			queen.push(i);
			queen.show();
			System.out.println();
		}
		System.out.println("\n================================================================================================================================================================");
		queen.show();
		System.out.println();
		for(int i=0; i<size;i++) {
			queen.poll();
			queen.show();
			System.out.println();
		}
		
			
	}
}
